﻿using System;
using System.Windows.Forms;
using NLog;
using System.Diagnostics;

namespace tuyaxia.MissJane
{
    public partial class Form : System.Windows.Forms.Form
    {
        SqliteLink dl;//Sqlite操作对象
        Logger log;//nlog
        Weather weather;//
        int lastNuUpVal = 1;//



        public Form()
        {
            InitializeComponent();
        }

        /// <summary>
        ///@ 输入检查安全.
        /// </summary>

        /// <summary>
        ///@ 输入体验优化
        /// </summary>    




        /// <summary>
        /// 计时器
        /// </summary>
        private void Timer1_Tick(object sender, EventArgs e)
        {
            // lab_date.Text = DateTime.Now.ToString();
            datePicker.Value = DateTime.Now;

        }

        /// <summary>
        /// bt_add_Click,获取输入,填充dataset
        /// </summary>
        private void bt_trAdd_Click(object sender, EventArgs e)
        {

            if (trNotNull()) {
                int price = 0;

                if (nud_suit.Value > 1)
                {
                    string date = datePicker.Value.ToString();//取时间
                    for (int i = 0; i < nud_suit.Value; i++)
                    {
                        dl.addRecord("groupTr", new string[] {date,
                                                              tlp.Controls.Find("tb_name" + i, false)[0].Text,
                                                              tlp.Controls.Find("nud_no" + i, false)[0].Text,
                                                              tlp.Controls.Find("tb_price" + i, false)[0].Text,
                                                              tlp.Controls.Find("cb_clothType" + i, false)[0].Text,
                                                              "0"
                                                                });
                        price += int.Parse(tlp.Controls.Find("tb_price" + i, false)[0].Text) * int.Parse(tlp.Controls.Find("nud_no" + i, false)[0].Text);

                    }
                    dl.addRecord("tr", new string[] { "null",
                                              date,
                                             cb_weather.SelectedItem.ToString(),
                                             tb_temperature.Text,
                                             nud_suit.Value+"件组合",
                                             "1",
                                             price.ToString(),
                                             tb_money.Text,
                                             cb_payType.SelectedItem.ToString(),
                                             "1",
                                             "组合",
                                             "0"
                                                });
                    log.Info("更新了多条记录{0}和统计记录{1}", dl.upData("groupTr"), dl.upData("tr"));

                }
                else {
                    dl.addRecord("tr", new string[] { "null",
                                              datePicker.Value.ToString(),
                                             cb_weather.SelectedItem.ToString(),
                                             tb_temperature.Text,
                                             tb_name0.Text,
                                             nud_no0.Value.ToString(),
                                             tb_price0.Text,
                                             tb_money.Text,
                                             cb_payType.SelectedItem.ToString(),
                                             "0",
                                             cb_clothType0.SelectedItem.ToString(),
                                             "0"
                                                });
                    log.Info("更新了{0}条记录.", dl.upData("tr"));
                }


                cb_count_SelectionChangeCommitted(null, null);

                trTextClear();
                timer1.Start();
            }


        }
        /// <summary>
        /// 输入检查,数字
        /// </summary>
        private void digitChk_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && e.KeyChar != 8 && e.KeyChar != 46)//数字,点和退格允许
            {
                e.Handled = true; return;

            }
            //TextBox t = ;
            if (e.KeyChar == 46) {
                if (((TextBox)sender).Text.Length == 0 || ((TextBox)sender).Text.Contains("."))//防止第1个点和多余的点
                {

                    e.Handled = true; return;
                }

                if (((TextBox)sender).Text.Length - ((TextBox)sender).SelectionStart >= 3)
                {

                    e.Handled = true; return;
                }
            }
            if (((TextBox)sender).Text.Length > 3 && ((TextBox)sender).Text.Contains(".") && e.KeyChar != 8)//防止小数点后大于２位
            {

                if (((TextBox)sender).Text.Length - ((TextBox)sender).SelectionStart <= 2 && ((TextBox)sender).Text.Length - ((TextBox)sender).Text.IndexOf('.') > 2)
                {

                    e.Handled = true; return; }

            }

        }

        private void Form_Load(object sender, EventArgs e)
        {
            log = LogManager.GetCurrentClassLogger();//nlog
            dateTimePicker1.Value = DateTime.Now;
            timer1.Start();//计时器开始
            dl = new SqliteLink("mj.db");//打开mj.db
            dl.readToComboBox("weatherType", cb_weather,-1);//读取weatherType表,填充到combox做为选项

            dl.readToComboBox("payType", cb_payType,-1);//读取payType表,填充到combox做为选项
            cb_payType.SelectedIndex = 0;
            dl.readToComboBox("stockType", cb_stockType,-1);
            cb_stockType.SelectedIndex = 0;
            dl.readToComboBox("sqlcom", cb_count,0);
            cb_count.SelectedIndex = 0;
            cb_count_SelectionChangeCommitted(null, null);
            dl.readToComboBox("clothType", cb_clothType0, -1);
            cb_clothType0.SelectedIndex = 0;
            dl.readToComboBox("customer", cb_customer, -1);
            cb_customer.SelectedIndex = 0;

            //天气
            weather = new Weather("衢州");
            if (cb_weather.Items.Contains(weather.getWeatherType()))
            {
                cb_weather.SelectedItem = weather.getWeatherType();
            }
            else
            {
                cb_weather.Items.Add(weather.getWeatherType());
                cb_weather.SelectedItem = weather.getWeatherType();
            }

            tb_temperature.Text = weather.getTemperature();


            tb_money.Focus();
        }

        private void rtb_log_TextChanged(object sender, EventArgs e)
        {
            if (rtb_log.Lines.Length > 5) {
                string[] sLines = rtb_log.Lines;
                string[] sNewLines = new string[sLines.Length - 1];

                Array.Copy(sLines, 1, sNewLines, 0, sNewLines.Length);

                rtb_log.Lines = sNewLines;

              
            }
        }
        public void trTextClear()
        {
            if (nud_suit.Value > 1) nud_suit.Value = 1;
            if (nud_no0.Value > 1) nud_no0.Value = 1;
            tb_name0.Text = "";
            tb_price0.Text = "";
            tb_money.Text = "";
            tb_money.Focus();
        }
        public bool trNotNull()
        {
            if (tb_money.Text == "" || tb_temperature.Text == "")
            {
                // log.Info("不能有空");
                return false;
            }
            for (int i = 0; i < nud_suit.Value; i++)
            {
                if (tlp.Controls.Find("tb_name" + i, false)[0].Text == "" || tlp.Controls.Find("tb_price" + i, false)[0].Text == "") return false;
            }
            return true;
        }

        public bool stockNotNull()
        {
            if (tb_stockPrice.Text == "" || tb_stockPriceOther.Text == "") return false;
            return true;
        }
        public void stockTextClear()
        {
            tb_stockPrice.Text ="";
            tb_stockPriceOther.Text = "";
            tb_stockPrice.Focus();
        }

        private void datePicker_Enter(object sender, EventArgs e)
        {
            timer1.Stop();
        }


        /// <summary>
        /// 调取数据库预存的sql,返回查询结果string
        /// </summary>
        private void cb_count_SelectionChangeCommitted(object sender, EventArgs e)
        {

            lab_count.Text = dl.salesCount(cb_count.SelectedItem.ToString());
        }

        /// <summary>
        /// nud_suit用来增删控件
        /// </summary>
        private void nuUp_ValueChanged(object sender, EventArgs e)
        {
            int val = (int)nud_suit.Value;
            while (lastNuUpVal < val)//增加控件
            {
                makeControl(lastNuUpVal);
                lastNuUpVal++;
            }
            while (lastNuUpVal > val)//删除控件
            {
                lastNuUpVal--;
                removeTextBox(lastNuUpVal);
            }


        }
        /// <summary>
        /// 增加控件
        /// </summary>
        private void makeControl(int row)
        {
            this.Size = new System.Drawing.Size(this.Width, this.Height + 30);
            tlp.Size = new System.Drawing.Size(tlp.Width, tlp.Height + 30);
            tlp.RowCount = row + 1;

            TextBox tbn = new TextBox();
            tbn.Name = "tb_name" + row;
            tbn.Size = new System.Drawing.Size(240, 25);
            tlp.Controls.Add(tbn, 0, row);

            NumericUpDown nud = new NumericUpDown();
            nud.Name = "nud_no" + row;
            nud.Size = new System.Drawing.Size(53, 25);
            nud.Minimum = 1;
            nud.Maximum = 10;
            tlp.Controls.Add(nud, 1, row);

            TextBox tbp = new TextBox();
            tbp.Name = "tb_price" + row;
            tbp.Size = new System.Drawing.Size(94, 25);
            tbp.KeyPress += new KeyPressEventHandler(this.digitChk_KeyPress);

            tlp.Controls.Add(tbp, 2, row);

            ComboBox cb = new ComboBox();
            cb.Name = "cb_clothType" + row;
            cb.Size = new System.Drawing.Size(84, 25);
            dl.readToComboBox("clothType", cb, -1);
            cb.SelectedIndex = 0;
            tlp.Controls.Add(cb, 3, row);
        }
        /// <summary>
        /// 删除控件
        /// </summary>
        private void removeTextBox(int row)
        {

            tlp.Controls.Remove(tlp.Controls.Find("tb_name" + row, false)[0]);
            tlp.Controls.Remove(tlp.Controls.Find("nud_no" + row, false)[0]);
            tlp.Controls.Remove(tlp.Controls.Find("tb_price" + row, false)[0]);
            tlp.Controls.Remove(tlp.Controls.Find("cb_clothType" + row, false)[0]);
            tlp.RowCount = row;
            this.tlp.Size = new System.Drawing.Size(tlp.Width, tlp.Height - 30);
            this.Size = new System.Drawing.Size(this.Width, this.Height - 30);




        }
        /// <summary>
        /// 双击日期标签,重置datePicker
        /// </summary>
        private void lab_date_DoubleClick(object sender, EventArgs e)
        {
            timer1.Start();
            datePicker.Value = DateTime.Now;
        }

        private void bt_stockAdd_Click(object sender, EventArgs e)
        {
            if (stockNotNull())
            {
                dl.addRecord("stock", new string[] {
                                              dateTimePicker1.Value.ToString(),
                                             tb_stockPrice.Text,
                                             tb_stockPriceOther.Text,
                                             cb_stockType.Text
                                                });
                log.Info("更新了{0}条记录.", dl.upData("stock"));
                stockTextClear();
            }
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (tabControl1.SelectedTab.Name)
            {
                case "tp_tr":
                    tpMove(tp_tr);
                    break;
                case "tp_stock":
                    tpMove(tp_stock);
                    break;
                case "tp_select":
                    tpMove(tp_select);
                    break;
            }
        }
        /// <summary>
        /// 标签事件时的控件移动
        /// </summary>
        private void tpMove(TabPage tp)
        {
            if (!tp.Controls.Contains(rtb_log))
                tp.Controls.Add(rtb_log);
        }

        private void tb_name_Leave(object sender, EventArgs e)
        {
            autoClothType( sender as TextBox);
        }
        private void autoClothType(TextBox tb)
        {
            if (tb.Text != "")
            {
              //  tb.Text. 
            }
                
        }

    }
 

}


   


